package com.xinkao.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.xinkao.example.config.annotation.SysLog;
import com.xinkao.example.service.RoleService;
import com.xinkao.example.util.CommonUtil;
import com.xinkao.example.util.RedisUtils;
import com.xinkao.example.util.constants.Constants;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ZSX
 * @Description 角色controller
 * @createTime 2021/1/14 18:51
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * @Author ZSX
     * @Description 查询角色列表
     * @Date 2021/01/14 15:30
     * @Param [requestJson]
     * @return JSONObject
     **/
    @SysLog(module = "角色模块", operate = "查询角色列表")
    @RequiresPermissions("role:list")
    @PostMapping("/list")
    public JSONObject listUser(@RequestBody JSONObject requestJson) {
        return roleService.listAllRole(requestJson);
    }

    /**
     * @Author ZSX
     * @Description 添加角色
     * @Date 2021/01/14 16:30
     * @Param [requestJson]
     * @return JSONObject
     **/
    @SysLog(module = "角色模块", operate = "保存")
    @RequiresPermissions("role:save")
    @PostMapping("/save")
    public JSONObject addRole(HttpServletRequest request,@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "roleName, permissionIds");
        String token = request.getHeader(Constants.AUTH);
        JSONObject user = redisUtils.get(token, JSONObject.class);
        return roleService.addRole(user,requestJson);
    }

    /**
     * @Author ZSX
     * @Description 修改角色
     * @Date 2021/01/14 16:30
     * @Param [requestJson]
     * @return JSONObject
     **/
    @SysLog(module = "角色模块", operate = "修改")
    @RequiresPermissions("role:edit")
    @PostMapping("/edit")
    public JSONObject updateRole(HttpServletRequest request,@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "roleId");
        String token = request.getHeader(Constants.AUTH);
        JSONObject user = redisUtils.get(token, JSONObject.class);
        return roleService.updateRole(user,requestJson);
    }

    /**
     * @Author ZSX
     * @Description 修改启用/禁用状态
     * @Date 2021/01/14 17:30
     * @Param [request]
     * @return JSONObject
     **/
    @SysLog(module = "角色模块", operate = "修改")
    @RequiresPermissions("role:delete")
    @GetMapping("/delete")
    public JSONObject deleteRole(HttpServletRequest request) {
        CommonUtil.hasAllRequired(CommonUtil.request2Json(request), "roleId");
        return roleService.deleteRole(CommonUtil.request2Json(request));
    }

}
